<?php




/**
* albumsizes Class
*/

class albumsizes
extends entity
{
	/**
	* Optional Constructor: Load on demand only.
	*/
	public function __construct()
	{
		# Parent's default constructor is necessary.
		parent::__construct();

		/**
		* Set Private, Protected or Public Members
		*/
		$this->protection_code = '5ed55c084a0fe23860ec99fe46d77ce9'; # Some random text, valid for the entire life
		$this->table_name = 'gallery_albums_sizes'; # Name of this table/entity name
		$this->pk_column = 'relationship_id'; # Primary Key's Column Name

		/**
		* Validation fields as used in add/edit forms
		*/
		$this->fields = array(
			# Remove the columns that you do not want to use in the ADD form
			'add' => array(
				'store_location' => null,
				'store_comments' => null,
			),
			
			# Remove the columns that you do not want to use in the EDIT form
			'edit' => array(
				'store_location' => null,
				'store_comments' => null,
			),
		);
	}

	/**
	* List entries from [ albumsizes ]
	* Column `code` signifies a protection code while deleting/editing a record
	* @param $conditions SQL Conditions
	* @return Multi-Dimensional array of entries in the list
	*/
	public function list_entries(condition $condition, $from_index=0, $per_page=50)
	{
		$crud = new crud();

		/**
		* Conditions are Compiled here so that we can manupulate them individually.
		* And make them fit for [ albumsizes ] only.
		*/
		$conditions_compiled_AND = $crud->compile_conditions(
			$condition->get_condition('AND'),
			false, 'AND', 1
		);
		$conditions_compiled_OR = $crud->compile_conditions(
			$condition->get_condition('OR'),
			false, 'OR', 2
		);

		$from_index = (int)$from_index;
		$per_page   = (int)$per_page;
		$variable   = new variable(); # It may be necessary to read list out data of a user

		$listing_sql="
SELECT SQL_CALC_FOUND_ROWS
	e.`{$this->pk_column}`, # Do not remove this
	
	e.album_id,
	e.size_id,
	
	# Modify these columns to your own required list (e.*)
	e.`store_location`,
	e.`store_comments`,
	
	# Flags, load them as per your need
	e.`is_approved`,
	
	MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_albums_sizes` `e`
WHERE
	(
		{$conditions_compiled_AND}
	)
	AND (
		{$conditions_compiled_OR}
	)
ORDER BY
	# We assume that the sorting fields are available
	#e.`sink_weight` ASC,
	e.album_id DESC,
	e.size_id ASC,
	e.`{$this->pk_column}` DESC
	#e.album_id DESC
LIMIT {$from_index}, {$per_page}
;";
		$this->query($listing_sql);
		$entries = $this->to_array();

		# Pagination helper: Set the number of entries
		$counter_sql="SELECT FOUND_ROWS() total;"; # Uses SQL_CALC_FOUND_ROWS from above query. So, run it immediately.
		$totals = $this->row($counter_sql);
		$this->total_entries_for_pagination = isset($totals['total'])?$totals['total']:0;

		return $entries;
	}
	
	/**
	* Details of an entity in [ albumsizes ] for management activities only.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function details($relationship_id=0)
	{
		global $subdomain_id;
		
		$relationship_id = (int)$relationship_id;
		$details_sql="
SELECT
	e.`{$this->pk_column}`, # Do not remove this

	e.*, # Modify these columns,
	
	# Admin must have it to EDIT the records
	MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_albums_sizes` `e`
WHERE
	e.`{$this->pk_column}` = {$relationship_id}
	AND e.is_active='Y'
	# AND e.subdomain_id={$subdomain_id}
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Details of an entity in [ albumsizes ] for public display.
	* @param $pk integer Primary Key's value of an entity
	* @return $details Associative Array of Detailed records of an entity
	*/
	public function get_details($relationship_id=0, $protection_code='')
	{
		$relationship_id = (int)$relationship_id;
		$protection_code = $this->sanitize($protection_code);
		$details_sql="
SELECT
	e.`{$this->pk_column}`, # Do not remove this

	e.*, # Modify these columns

	MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) `code` # Protection Code
FROM `gallery_albums_sizes` `e`
WHERE
	e.`{$this->pk_column}` = {$relationship_id}
	AND e.is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(e.`{$this->pk_column}`, '{$this->protection_code}')) = '{$protection_code}'
;";
		$details = $this->row($details_sql);
		return $details;
	}

	/**
	* Flag a field; dummy use; unless you use it.
	* Every method should sanitize the user input.
	* It will co-exist with the live features.
	*/
	public function flag_field($relationship_id=0, $protection_code='', $field_name='')
	{
		# Allow only selected fields to be flaged Y/N
		if(!in_array($field_name, array('is_approved', 'is_featured')))
		{
			# Such flag does not exist.
			return false;
		}
		
		$relationship_id = (int)$relationship_id;
		$protection_code = $this->sanitize($protection_code);
		global $subdomain_id;

		$flag_sql="
UPDATE `gallery_albums_sizes` SET
	# Set your flag name here
	`{$field_name}`=IF(`{$field_name}`='Y', 'N', 'Y')
WHERE
	`{$this->pk_column}` = {$relationship_id}
	AND subdomain_id={$subdomain_id}
	
	# Don't touch the deleted flags
	AND is_active='Y'

	# Optionally validate
	AND MD5(CONCAT(`{$this->pk_column}`, '{$this->protection_code}')) = '{$protection_code}'
;";
		return $this->query($flag_sql);
		#return false; # dummy mode only
	}

	/**
	* Welcome and ask for authentication?
	* Please extend this method according to your business logic.
	* eg. Send email to the first signed up member, trigger something else when a data is added.
	* Called right after a new [ albumsizes ] is added: insert-hook.
	*/
	public function welcome_first($relationship_id=0)
	{
		$relationship_id = (int)$relationship_id;
	 	
		return true;
	}
	
	/**
	* Find out where is the album size stored at
	*/
	function store_location($album_id=0, $size_id=0)
	{
		$album_id = (int)$album_id;
		$size_id  = (int)$size_id;
		$location_sql="SELECT store_location FROM gallery_albums_sizes WHERE album_id={$album_id} AND size_id={$size_id};";
		$location = $this->row($location_sql);
		if(!isset($location['store_location'])) $location = array('store_location' => '/tmp');
		return $location['store_location'];
	}
	
	/**
	* Originally scan an album location on the server
	*/
	public function add_album_size_originally_scanned($album_id=0, $store_location='/tmp', $size_id=0)
	{
		$album_id = (int)$album_id;
		$size_id = (int)$size_id;
		$bytes = 0; # is_dir($store_location)?disk_total_space($store_location):0;
		global $subdomain_id;
		
		if(!is_dir($store_location)) mkdir($store_location, 0777, true);
		
		$originally_scanned_sql="
INSERT INTO gallery_albums_sizes (
	album_id, size_id,
	subdomain_id, added_on,
	is_active, is_approved,
	store_bytes, store_location,
	store_comments
) VALUES (
	{$album_id}, {$size_id},
	{$subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()),
	'Y', 'Y',
	{$bytes}, '{$store_location}',
	'Originally scanned'
) ON DUPLICATE KEY UPDATE
	store_location='{$store_location}',
	modified_on=UNIX_TIMESTAMP(CURRENT_TIMESTAMP()),
	modified_counter=modified_counter+1,
	store_comments='Manipulated'
;";
		return $this->query($originally_scanned_sql);
	}
	
	/**
	* Applies all default thumbnail sizes to a freshly added album
	*/
	public function assign_all_default_sizes($album_id=0)
	{
		$album_id = (int)$album_id;
		global $subdomain_id;
		
		# Get the listof default thumbnail sizes
		$originally_scanned = THUMBNAIL_ORIGINAL_SIZE_ID;
		$sizes_sql="SELECT size_id FROM gallery_sizes WHERE is_active='Y' AND is_approved='Y' AND is_default='Y' AND size_id!={$originally_scanned};";
		$sizes = $this->arrays($sizes_sql);
		#print_r($sizes); #die();
		foreach($sizes as $s => $size)
		{
			#print_r($size); #die();
			$store_location = FTPPATH_THUMBNAILS."/{$album_id}/{$size['size_id']}";
			$size_id_sql="
INSERT IGNORE INTO gallery_albums_sizes (
	album_id, size_id,
	subdomain_id, added_on,
	is_active, is_approved,
	store_location,
	store_comments
) VALUES (
	{$album_id}, {$size['size_id']},
	{$subdomain_id}, UNIX_TIMESTAMP(CURRENT_TIMESTAMP()),
	'Y', 'Y',
	'{$store_location}',
	'Auto applied'
);";
			#die($size_id_sql);
			$this->query($size_id_sql);
		}
	}
}
?>